<?php
// +----------------------------------------------------------------------
// | Author: 发仔 <i@fazi.me> 
// +----------------------------------------------------------------------

namespace app\admin\controller;

use think\Db;

class Module extends Base
{
    public function index()
    {
    	$all = Db::name('module')->select();
    	
    	//树
    	$list = Arr2Tree($all);
//    	halt($list);
    	$parents = array_column($all,'title','id');
    	
    	$this->assign('list',$list);
    	$this->assign('parents',$parents);
    	
    	return $this->out();
    	
    }
    
    public function add() {
    	
    	$P = Input('post.');
    	if( $P ) {
			
			$rule = [
				'name' => 'require|alpha',
				'title' => 'require|min:2',
			];
			$rslt = $this->validate($P,$rule);
			if( true !== $rslt ) {
				return $this->error($rslt);
			}
			
			$data = [
				'name' => ucwords(trim($P['name'])),
				'title' => trim($P['title']),
				'pid' => intval($P['pid']),
				'state' => intval($P['state']),
			];
			
			$rslt = Db::name('module')->insert($data);
			
			return $this->res($rslt);
		
		}
		
		return $this->out();
    	
	}
    
    public function edit() {
		
		$id = input('praram.id');
		$item = Db::name('role')->find();
		
		$this->assign('item',$item);
		return $this->out('item');
		
	}
    
}
